package edu.nwpu.zya.coreops.modules.resource.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import edu.nwpu.zya.coreops.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import edu.nwpu.zya.coreops.modules.resource.entity.AppEntity;
import edu.nwpu.zya.coreops.modules.resource.service.AppService;
import edu.nwpu.zya.coreops.common.utils.PageUtils;
import edu.nwpu.zya.coreops.common.utils.R;



/**
 * 
 *
 * @author zhouyuang
 * @email 742025491@qq.com
 * @date 2022-05-23 16:02:47
 */
@RestController
@RequestMapping("resource/app")
public class AppController {
    @Autowired
    private AppService appService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("resource:app:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("userId", ShiroUtils.getUserId());
        PageUtils page = appService.queryRdsVoPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("resource:app:info")
    public R info(@PathVariable("id") Long id){
		AppEntity app = appService.getById(id);

        return R.ok().put("app", app);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("resource:app:save")
    public R save(@RequestBody AppEntity app){
        app.setInstanceId(NanoIdUtils.randomNanoId());
        app.setUserId(ShiroUtils.getUserId());
		appService.saveApp(app);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("resource:app:update")
    public R update(@RequestBody AppEntity app){
		appService.updateByApp(app);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("resource:app:delete")
    public R delete(@RequestBody Long[] ids){
		appService.removeRdsByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取主机和各个指标的关联信息
     * @param params
     * @return
     */
    @RequestMapping("/metric/list")
    public R metricList(@RequestParam Map<String, Object> params){
        PageUtils page = appService.queryMetricVoPageByType(params);

        return R.ok().put("page",page);
    }

    /**
     * 添加主机和各个指标的关联信息
     * @param paramString
     * @return
     */
    @PostMapping("/metric/add")
    public R metricAdd(@RequestBody String paramString){
        JSONObject jsonObject = JSONObject.parseObject(paramString);
        Long appId = jsonObject.getLong("appId");
        JSONArray jsonArray = jsonObject.getJSONArray("metricIds");
        List<Long> metricIds = jsonArray.toJavaList(Long.class);
        appService.addMetrics(appId,metricIds);

        return R.ok();
    }

    /**
     * 删除主机和各个指标的关联信息
     * @param paramString
     * @return
     */
    @PostMapping("/metric/delete")
    public R metricDelete(@RequestBody String paramString){
        JSONObject jsonObject = JSONObject.parseObject(paramString);
        Long appId = jsonObject.getLong("appId");
        JSONArray jsonArray = jsonObject.getJSONArray("metricIds");
        List<Long> metricIds = jsonArray.toJavaList(Long.class);
        appService.cancelMetrics(appId,metricIds);

        return R.ok();
    }

}
